/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.global;

import cn.ac.iie.di.ipparser.RangeSearch;
import cn.ac.iie.di.rocketmq.Message;
import cn.ac.iie.di.tools.Configuration;
import cn.ac.iie.di.tools.TRSTools;
import static cn.ac.iie.di.tools.TimeFormat.dateToStamp;
import static cn.ac.iie.di.tools.TimeFormat.getMqTime;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author lenovo-pc
 */
public class GlobalParas {

    static private final Logger logger = Logger.getLogger(GlobalParas.class.getName());

    public static String IP_PORT = null;

    public static Long GET_MQ_TIME = null;
    public static String POLL_MQ_NAME = null;
    public static String MQ_NEWS1 = null;
    public static String MQ_NEWS2 = null;
    public static String MQ_COMMENT1 = null;
    public static String MQ_COMMENT2 = null;
    public static String MQ_USER = null;

//    消息队列名称
    public static String MQ_APP_STROE = null;
    public static String MQ_APP_CATEGORY = null;
    public static String MQ_APP_INFO = null;
    public static String MQ_APP_COMMENT = null;
    public static String MQ_APP_DEVELOPER = null;
    
    public static String TYPE_NEWS = null;
    public static String TYPE_COMMENT = null;
    public static String TYPE_USER = null;
    
//    字段类型
//    应用商店
    public static String TYPE_APP_STORE = null;
    public static String TYPE_APP_CATEGORY = null;
    public static String TYPE_APP_INFO = null;
    public static String TYPE_APP_COMMENT = null;
    public static String TYPE_APP_DEVELOPER = null;
    
    
    public static ArrayBlockingQueue<Map> NEWS1_QUEUE = null;
    public static ArrayBlockingQueue<Map> NEWS2_QUEUE = null;
    public static ArrayBlockingQueue<Map> COMMENT1_QUEUE = null;
    public static ArrayBlockingQueue<Map> COMMENT2_QUEUE = null;
    public static ArrayBlockingQueue<Map> USER_QUEUE = null;
    public static ArrayBlockingQueue<Message> PULL_MQ_QUEUE = null;
  
//    缓存队列
    //APP应用商店
    public static ArrayBlockingQueue<Map> APP_STORE_QUEUE = null;
    //APP应用分类
    public static ArrayBlockingQueue<Map> APP_CATEGORY_QUEUE = null;
    //APP相关信息
    public static ArrayBlockingQueue<Map> APP_INFO_QUEUE = null;
    //APP评论
    public static ArrayBlockingQueue<Map> APP_COMMENT_QUEUE = null;
    //APP开发商信息
    public static ArrayBlockingQueue<Map> APP_DEVELOPER_QUEUE = null;
    
    
    
    
    
    
    
    
    
    
    public static Integer POOL_MSG = null;

    public static TRSTools TRS_TOOLS = null;

    public static String FILE_PATH = null;
    public static String APP_FILE_PATH = null;
    
    public static int PARSER_POOL = 0;
    public static int SEND_POOL = 0;

    public static RangeSearch rs1 = null;
    public static HashMap<String, Integer> id2Provinces = new HashMap();

    public static HashMap<String, String> SITE_NAME = null;
    public static String RM_DUP = null;

    private static final HashMap<String, String> propMap = new HashMap<>();
    private static final Configuration conf = Configuration.getConfiguration(ClassLoader.getSystemClassLoader().getResource("config.properties").getFile());
    public static int QUEUE_SIZE;

    static {
//    public static void init() {
        try {
            //MQ端口号
            IP_PORT = LoadProperties("IP_PORT");
            //消息队列名称
            GET_MQ_TIME = getMqTime(LoadProperties("GET_MQ_TIME"));
            POLL_MQ_NAME = LoadProperties("POLL_MQ_NAME");
            MQ_NEWS1 = LoadProperties("MQ_NEWS1");
            MQ_NEWS2 = LoadProperties("MQ_NEWS2");
            MQ_COMMENT1 = LoadProperties("MQ_COMMENT1");
            MQ_COMMENT2 = LoadProperties("MQ_COMMENT2");
            MQ_USER = LoadProperties("MQ_USER");
            MQ_APP_STROE = LoadProperties("MQ_APP_STROE");
            MQ_APP_CATEGORY = LoadProperties("MQ_APP_CATEGORY");
            MQ_APP_INFO = LoadProperties("MQ_APP_INFO");
            MQ_APP_COMMENT = LoadProperties("MQ_APP_COMMENT");
            MQ_APP_DEVELOPER = LoadProperties("MQ_APP_DEVELOPER");
      
      
            //解析字段
            TYPE_NEWS = LoadProperties("TYPE_NEWS");
            TYPE_COMMENT = LoadProperties("TYPE_COMMENT");
            TYPE_USER = LoadProperties("TYPE_USER");
            TYPE_APP_STORE = LoadProperties("TYPE_APP_STORE");
            TYPE_APP_CATEGORY = LoadProperties("TYPE_APP_CATEGORY");
            TYPE_APP_INFO = LoadProperties("TYPE_APP_INFO");
            TYPE_APP_COMMENT = LoadProperties("TYPE_APP_COMMENT");        
            TYPE_APP_DEVELOPER = LoadProperties("TYPE_APP_DEVELOPER");   
            logger.info("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+TYPE_APP_DEVELOPER);
                    
            
            //设置缓存队列大小
            NEWS1_QUEUE = new ArrayBlockingQueue(Integer.parseInt(LoadProperties("QUEUE_SIZE")));
            NEWS2_QUEUE = new ArrayBlockingQueue(Integer.parseInt(LoadProperties("QUEUE_SIZE")));
            COMMENT1_QUEUE = new ArrayBlockingQueue(Integer.parseInt(LoadProperties("QUEUE_SIZE")));
            COMMENT2_QUEUE = new ArrayBlockingQueue(Integer.parseInt(LoadProperties("QUEUE_SIZE")));
            USER_QUEUE = new ArrayBlockingQueue(Integer.parseInt(LoadProperties("QUEUE_SIZE")));
            APP_STORE_QUEUE  = new ArrayBlockingQueue(Integer.parseInt(LoadProperties("QUEUE_SIZE")));
            APP_CATEGORY_QUEUE  = new ArrayBlockingQueue(Integer.parseInt(LoadProperties("QUEUE_SIZE")));
            APP_INFO_QUEUE  = new ArrayBlockingQueue(Integer.parseInt(LoadProperties("QUEUE_SIZE")));
            APP_COMMENT_QUEUE  = new ArrayBlockingQueue(Integer.parseInt(LoadProperties("QUEUE_SIZE")));
            APP_DEVELOPER_QUEUE  = new ArrayBlockingQueue(Integer.parseInt(LoadProperties("QUEUE_SIZE")));
            
            QUEUE_SIZE = Integer.parseInt(LoadProperties("QUEUE_SIZE"));
            PULL_MQ_QUEUE = new ArrayBlockingQueue(Integer.parseInt(LoadProperties("PULL_MQ_QUEUE")));

            POOL_MSG = Integer.parseInt(LoadProperties("POOL_MSG"));
            String trs = LoadProperties("TRS_URL");
            TRS_TOOLS = new TRSTools(trs);

            FILE_PATH = LoadProperties("FILE_PATH");
            APP_FILE_PATH = LoadProperties("APP_FILE_PATH");
            PARSER_POOL = Integer.parseInt(LoadProperties("PARSER_POOL"));
            SEND_POOL = Integer.parseInt(LoadProperties("SEND_POOL"));
            RM_DUP = LoadProperties("RM_DUP");

            logger.debug("IP_PORT: " + IP_PORT + " GET_MQ_TIME: " + GET_MQ_TIME + " POLL_MQ_NAME: " + POLL_MQ_NAME + " MQ_NEWS1: " + MQ_NEWS1 + " QUEUE_SIZE: " + QUEUE_SIZE + " PULL_MQ_QUEUE：" + PULL_MQ_QUEUE + " TRS_URL: " + trs);

            readId2Province();
            try {
                ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(ClassLoader.getSystemClassLoader().getResource("IPGeoLocator.dat").getFile())));
                rs1 = (RangeSearch) in.readObject();
            } catch (IOException ex) {
                logger.error(ex, ex);
            } catch (ClassNotFoundException ex) {
                logger.error(ex, ex);
            }
            try {
                siteName();
            } catch (Exception e) {
                logger.error(e, e);
            }

        } catch (Exception ex) {
            logger.error(ex, ex);
        }
    }

    public static String LoadProperties(String str) {
        if (propMap.containsKey(str)) {
        } else {
            synchronized (conf) {
                if (propMap.containsKey(str)) {
                } else {
                    propMap.put(str, conf.getString(str, ""));
                }
            }
        }
        return propMap.get(str);
    }

    public static void readId2Province() throws IOException {
//        BufferedReader br = new BufferedReader(new FileReader(ClassLoader.getSystemClassLoader().getResource("log4j.properties").getFile()));
        BufferedReader br = null;
        try {
            File file = new File(ClassLoader.getSystemClassLoader().getResource("ip2province").getFile());
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] id2Province = line.split("[|]");
                GlobalParas.id2Provinces.put(id2Province[0].trim(), Integer.parseInt(id2Province[1].trim()));
            }
            logger.info("GlobalParas.id2Provinces size" + GlobalParas.id2Provinces.size());
        } catch (Exception e) {
            logger.error(e, e);
            System.exit(0);
        } finally {
            br.close();
        }
    }

    public static void siteName() throws InterruptedException {
        GlobalParas.SITE_NAME = new HashMap<String, String>();
//        while (true) {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(ClassLoader.getSystemClassLoader().getResource("sitename.properties").getFile()));
            String line;
            while ((line = br.readLine()) != null) {
                String[] jw = line.split(":");
                SITE_NAME.put(jw[0], jw[1]);
            }
            br.close();
        } catch (Exception e) {
        } finally {
            try {
                br.close();
            } catch (IOException ex) {
                java.util.logging.Logger.getLogger(GlobalParas.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
//            Thread.sleep(1800000);
//        }

    }

    public static void main(String[] args) {
    }

}
